package com.uchedao.seller.ui.carlist.m_service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.uchedao.seller.ui.carlist.db.CarDbHelper;
import com.uchedao.seller.ui.carlist.entity.BrandEntity;
import com.uchedao.seller.ui.carlist.entity.ModelEntity;
import com.uchedao.seller.ui.carlist.entity.SeriesEntity;
import com.uchedao.seller.ui.carlist.response.MyBrandResponse;
import com.uchedao.seller.ui.carlist.response.MyModelResponse;
import com.uchedao.seller.ui.carlist.response.MySeriesResponse;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by ZhangZhanghua on 2015/10/28 0028.
 */
public class UpdateDbService extends Service {
    private static CarDbHelper carDbHelper;
    private static ArrayList<BrandEntity> brandList;
    private static ArrayList<SeriesEntity> seriesList;
    private static ArrayList<ModelEntity> modelList;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e("tag","开始下载");
        final RequestQueue queue = Volley.newRequestQueue(UpdateDbService.this);
        String url = "http://testopenapi.uchedao.com/base/vehicle/brands";
        final String url1 = "http://testopenapi.uchedao.com/base/vehicle/serieses?brand_id=";
        final String url2 = "http://testopenapi.uchedao.com/base/vehicle/models?series_id=";
        carDbHelper = CarDbHelper.getInstance(this);
        seriesList = new ArrayList<SeriesEntity>();
        modelList = new ArrayList<ModelEntity>();
        carDbHelper.deleteAllTable();
        //根据给定的URL新建一个请求
        final JsonRequest brandRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                MyBrandResponse myBrandResponse = new Gson().fromJson(response.toString(), MyBrandResponse.class);
                brandList = (ArrayList<BrandEntity>) myBrandResponse.getData().getList();
                for (BrandEntity brand : brandList) {
                    carDbHelper.insertBrand(brand.getId(), brand.getName(), brand.getFull_spell(), brand.getInitial(), brand.getIcon_path(), brand.getIs_hot());
                }
                for (int i = 0; i < brandList.size(); i++) {
                    final JsonRequest seriesRequest = new JsonObjectRequest(url1 + brandList.get(i).getId(), new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.e("series",response+"");
                            MySeriesResponse mySeriesResponse = new Gson().fromJson(response.toString(), MySeriesResponse.class);
                            seriesList = (ArrayList<SeriesEntity>) mySeriesResponse.getData().getList();
                            for (SeriesEntity series : seriesList) {
                                carDbHelper.insertSeries(series.getId(), series.getBrand_id(), series.getName(), series.getFull_spell(), series.getType_id(), series.getImport_id());
                            }
                            for (int j = 0; j < seriesList.size(); j++) {
                                JsonRequest modelRequest = new JsonObjectRequest(url2+seriesList.get(j).getId(), new Response.Listener<JSONObject>() {
                                    @Override
                                    public void onResponse(JSONObject response) {
                                        Log.e("model",response+"");
                                        MyModelResponse myModelResponse = new Gson().fromJson(response.toString(), MyModelResponse.class);
                                        modelList = (ArrayList<ModelEntity>) myModelResponse.getData().getList();
                                        for (ModelEntity model : modelList) {
                                            carDbHelper.insertModel(model.getId(), model.getSeries_id(), model.getName(), model.getModel_year());

                                        }
                                    }
                                }, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {

                                    }
                                });
                                modelRequest.setTag("model");
                                queue.add(modelRequest);
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
                    seriesRequest.setTag("series");
                    queue.add(seriesRequest);
                    if (i==brandList.size()){
                        carDbHelper.setTransactionSuccessful();
                        carDbHelper.endTransaction();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        brandRequest.setTag("brand");
        queue.add(brandRequest);
    }

}
